package util;

import java.util.ArrayList;
import java.util.List;
import common.Constants;
import common.Operation;

public class SearchCommandsLastfm extends SearchCommands {
	
	// Patrón para el parámetro pasado al comando
	private static final String PARAM_COMMAND_PATTERN = ":[A-Za-z0-9]*";
	
	//Comandos
	private static final String GET_NEIGHBOURS_COMMAND = "@lw.neighbours";
	private static final String GET_RECENT_TRACKS_COMMAND = "@lw.recent-tracks";
	private static final String GET_TOP_ALBUMS_COMMAND = "@lw.top-albums";
	private static final String GET_TOP_ARTIST_COMMAND = "@lw.top-artist";
	
	private static final String CREATE_PLAYLIST_COMMAND = "@lw.create-playlist";
	private static final String ADD_TRACK_COMMAND = "@lw.add-track";
	private static final String GET_PLAYLIST_COMMAND = "@lw.show-playlist";
	
	
	
		
	private static SearchCommandsLastfm instance;
	
	private String [] commandsNoParams = {CREATE_PLAYLIST_COMMAND, ADD_TRACK_COMMAND,
											GET_PLAYLIST_COMMAND};
	
	private String [] commandsWithParams = {GET_NEIGHBOURS_COMMAND, GET_RECENT_TRACKS_COMMAND,
											GET_TOP_ALBUMS_COMMAND, GET_TOP_ARTIST_COMMAND};
	
	private List<String> commandsPattern;
	
	private SearchCommandsLastfm() {
		commandsPattern = createCommandsPatterns(commandsWithParams, commandsNoParams);
	}
	
	private List<String> createCommandsPatterns(String [] commandsWithParams, String [] commandsNoParams) {
		List<String> result = new ArrayList<String>(commandsWithParams.length + commandsNoParams.length);
		for (String command : commandsNoParams) {
			result.add(command);
		}
		for (String command : commandsWithParams) {
			StringBuilder sb = new StringBuilder(command.length() + PARAM_COMMAND_PATTERN.length());
			sb.append(command).append(PARAM_COMMAND_PATTERN);
			result.add(sb.toString());
		}
		return result;
	}

	public static SearchCommandsLastfm getInstance() {
		if (instance == null) {
			instance = new SearchCommandsLastfm();
		}
		return instance;
	}

	/**
	 * Recupera la lista de patrones de lo comandos.
	 */
	public List<String> getCommandsPatterns() {
		return commandsPattern;
	}
	
	/**
	 * REcupera una lista de operaciones a partir de un lista de comandos
	 * @param commands lista de comandos.
	 * @return lista de operaciones
	 */
	public List<Operation> getOperationsFromCommands(List<String> commands) {
		List<Operation> result = new ArrayList<Operation>(commands.size());
		for (String command : commands) {
			Operation operation = null;
			if (command.contains(CREATE_PLAYLIST_COMMAND)) {
				operation = new Operation(Constants.TYPE_OF_OPERATION.CREATE_PLAYLIST);
			} else if (command.contains(ADD_TRACK_COMMAND)) {
				operation = new Operation(Constants.TYPE_OF_OPERATION.ADD_TRACK);
			} else if (command.contains(GET_PLAYLIST_COMMAND)) {
				operation = new Operation(Constants.TYPE_OF_OPERATION.GET_PLAYLIST);
			} else if (command.contains(GET_NEIGHBOURS_COMMAND)) {
				operation = new Operation(Constants.TYPE_OF_OPERATION.GET_NEIGHBOURS);
				String [] commandParam = command.split(":");
				if (commandParam.length == 2) {
					operation.addParam(Operation.USER_PARAM, commandParam[1]);
				}
			} else if (command.contains(GET_RECENT_TRACKS_COMMAND)) {
				operation = new Operation(Constants.TYPE_OF_OPERATION.GET_RECENT_TRACKS);
				String [] commandParam = command.split(":");
				if (commandParam.length == 2) {
					operation.addParam(Operation.USER_PARAM, commandParam[1]);
				}
			} else if (command.contains(GET_TOP_ALBUMS_COMMAND)) {
				operation = new Operation(Constants.TYPE_OF_OPERATION.GET_TOP_ALBUMS);
				String [] commandParam = command.split(":");
				if (commandParam.length == 2) {
					operation.addParam(Operation.USER_PARAM, commandParam[1]);
				}
			} else if (command.contains(GET_TOP_ARTIST_COMMAND)) {
				operation = new Operation(Constants.TYPE_OF_OPERATION.GET_TOP_ARTIST);
				String [] commandParam = command.split(":");
				if (commandParam.length == 2) {
					operation.addParam(Operation.USER_PARAM, commandParam[1]);
				}
			}
			result.add(operation);
		}
		return result;
	}

}
